﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Threading;
using Contracts;

namespace ViewModel
{
    public class DiscussionViewModel : ClosableViewModel
    {
        public Contact CurrentContact { get; set; }
        public ObservableCollection<Message> Messages { get { return this.messages; } }
        private ObservableCollection<Message> messages;

        public DiscussionViewModel(Contact contact)
        {
            this.CurrentContact = contact;
            this.DisplayName = string.Format("Conversation with {0}", contact.Alias);
            this.messages = new ObservableCollection<Message>();
        }

        public void AddMessage(Message message)
        {
            Application app = System.Windows.Application.Current;
            if (app != null)
                app.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(AsyncAdd), message);
        }

        private object AsyncAdd(object message)
        {
            this.messages.Add((Message)message);
            return null;
        }

        public override string ToString()
        {
            return this.CurrentContact.Alias;
        }
    }
}
